<!-- 屈光发育档案/角塑复查(新增/修改) -->
<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" center append-to-body fullscreen>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-tabs type="border-card" v-model="activeName">
      <el-tab-pane label="常规检查" name="常规检查">
        <ck-corneal ref="oneTab" @refreshData="getImages" @dateChange="checkDateHandle"></ck-corneal>
      </el-tab-pane>
      <el-tab-pane label="影像采集" name="影像采集">
        <image-collect ref="fourTab" @refreshDataList="getDataList"></image-collect>
      </el-tab-pane>
    </el-tabs>
		<span slot="footer" class="dialog-footer">
			<el-button size="medium" type="primary" @click="getDataForm">保存</el-button>
		</span>
  </el-dialog>
</template>

<script>
	import { filterParams } from '@/util/tools'
  import {getCkCornealObj, addCkCornealObj, putCkCornealObj} from '@/api/pvm/ckcorneal'
  import CkCorneal from './ckcorneal-form'
  import ImageCollect from './image-collect'
  export default {
    data(){
      return {
        visible: false,
        activeName: '',
        form1: {},
        form4: {},
        dataForm: {
          personInfo:{},
          optometry:{},
          image: {}
        },
        images: []
      }
    },
    components: {
      CkCorneal,
      ImageCollect
    },
    methods: {
      init(id,checkType,name,docId,customerId){
        this.visible = true
        this.dataForm.id = id || 0
        this.dataForm.checkType = checkType || 1
        if(!docId){
          docId = (new Date()).valueOf()
        }
        this.images = []
        this.activeName = name

        this.customerId = customerId
        this.docId = docId
        this.$nextTick(() => {
          if(customerId){
            this.$refs.oneTab.init(this.dataForm.id,this.dataForm.checkType,docId,customerId)
            this.$refs.fourTab.init(this.dataForm.id,docId,customerId,this.images)
          }else{
            this.$refs.oneTab.init(this.dataForm.id,this.dataForm.checkType,docId)
            this.$refs.fourTab.init(this.dataForm.id,docId,'',this.images)
          }
        })
      },
      checkDateHandle(data){
        if(this.customerId){
          this.$refs.fourTab.init(this.dataForm.id,this.docId,this.customerId,this.images,data)
        }else{
          this.$refs.fourTab.init(this.dataForm.id,this.docId,'',this.images,data)
        }
      },
      getDataForm(){
        this.$nextTick(() => {
          let personInfo = this.$refs.oneTab.personInfo
          let form1 = this.$refs.oneTab.dataForm
          let form4 = this.$refs.fourTab.dataForm
          form1.docId = form4.docId
          let images = this.$refs.fourTab.images || []
          let imagesList = this.$refs.fourTab.imgList || []
          this.dataForm.personInfo = personInfo
          let str = JSON.stringify(form1)
          this.dataForm.ckCorneal = filterParams(JSON.parse(str))
          this.dataForm.ckCorneal.images = images.concat(imagesList)
          this.dataForm.image = form4
          this.dataFormSubmit();
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs.oneTab.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putCkCornealObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.mdg)
                }
              })
            } else {
              addCkCornealObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.mdg)
                }
              })
            }
          }
        })
      },
      getImages(data){
        this.images = data
      },
      getDataList(){
        this.visible = false
        this.$emit('refreshDataList')
      }
    }
  }
</script>

<style>
</style>
